/**
 * Copyright (C) 2011-2012 GreenSoft S.A.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.greensoft.neosystem.common;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;

/**
 * 
 * @author Jorge E. Villaverde
 * @date 05/01/2012
 * @version 1.0.0
 */
@SessionScoped
@Named
public class MenuResolver implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Inject
	private Logger logger;
	
	private CompositeMenuItem rootMenu;
	private List<MenuItem> breadCrums;
	private MenuItem selectedMenuItem;
	private MenuItem selectedBreadCrum;
	
	@PostConstruct
	public void buildMenuItems() {
		rootMenu = new CompositeMenuItem("home", "/index.xhtml");
		
		rootMenu.addMenuItem(new CrudMenu("Persona", "/crm/personaFisica"));
		rootMenu.addMenuItem(new CrudMenu("Empresa", "/crm/personaJuridica"));
		
		selectedMenuItem = rootMenu;
		this.breadCrums = new ArrayList<MenuItem>();
	}

	@Produces
	@Named
	public List<MenuItem> getUserMenuItems() {
		return selectedMenuItem.getSubItems();
	}

	@Produces
	@Named
	public List<MenuItem> getBreadCrums() {
		return breadCrums;
	}

	@Produces
	@Named
	public MenuItem getSelectedMenuItem() {
		return selectedMenuItem;
	}

	public void setSelectedMenuItem(MenuItem selectedMenuItem) {
		breadCrums.add(this.selectedMenuItem);
		this.selectedMenuItem = selectedMenuItem;
	}
	
	protected String decoreMenu(String action){
		return action + "?faces-redirect=true&amp;includeViewParams=true";
	}
	
	public String showMenu(){
		if(selectedMenuItem != null){
			logger.info("Select Menu: " + selectedMenuItem.getLabel());
			return decoreMenu(selectedMenuItem.getAction());
		}
		logger.info("No Selected Menu, using root");
		return decoreMenu(rootMenu.getAction());
	}
	
	public String restoreBreadCrum(){
		int indexOfBreadCrumb = breadCrums.indexOf(selectedBreadCrum);
		if(indexOfBreadCrumb != -1){
			for(int i=breadCrums.size()-1; i >= indexOfBreadCrumb;i--){
				breadCrums.remove(i);
			}
		}		
		selectedMenuItem = selectedBreadCrum;
		return showMenu();
	}

	public String goBack(){
		selectedBreadCrum = breadCrums.get(breadCrums.size()-1);
		return restoreBreadCrum();
	}
	
	public MenuItem getSelectedBreadCrum() {
		return selectedBreadCrum;
	}

	public void setSelectedBreadCrum(MenuItem selectedBreadCrum) {
		this.selectedBreadCrum = selectedBreadCrum;
	}

}
